package com.nexgo.payment.trans.setting.terminalkey;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.Spinner;

import com.nexgo.payment.R;
import com.nexgo.payment.constrant.PreferencesConstants;
import com.nexgo.payment.databases.Repository;
import com.nexgo.payment.trans.setting.SaveParameterActivity;
import com.nexgo.payment.trans.setting.SaveParameterPresenter;
import com.nexgo.payment.util.StringUtils;
import com.nexgo.payment.util.UIUtils;
import com.nexgo.payment.widget.SwitchButton;
import com.nexgo.payment.widget.TitleBuilder;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 终端密钥设置
 */
public class SetupMasterKeyActivity extends SaveParameterActivity implements AdapterView.OnItemSelectedListener {

    @BindView(R.id.master_key_index)
    EditText masterKeyIndex;
    @BindView(R.id.track_encrypted)
    SwitchButton trackEncrypted;
    @BindView(R.id.check_sm_algorithm)
    SwitchButton checkSmAlgorithm;
    @BindView(R.id.spinner_tmkalgo_selection)
    Spinner tmkalgoSelection;

    private String mKeyIndex;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setup_master_key);
        ButterKnife.bind(this);
        new SaveParameterPresenter(this, Repository.getInstance());
        //绑定事件回调
        tmkalgoSelection.setOnItemSelectedListener(this);

        initTitle();
    }

    //初始化标题栏
    private void initTitle() {
        new TitleBuilder(this)
                .setExternalTitleBgRes(R.drawable.bg_blue_icon)
                .setLeftImage(R.drawable.arrow_icon)
                .setRightText(getString(R.string.action_save))
                .setTitleText(getString(R.string.setup_master_key))
                .build();
    }

    @Override
    protected HashMap<String, String> getDataBind() {
        HashMap<String, String> map = new HashMap<>();
        map.put(PreferencesConstants.MASTER_KEY_INDEX, masterKeyIndex.getText().toString());
        map.put(PreferencesConstants.MASTER_KEY_ALGORITHM, mKeyIndex);
        map.put(PreferencesConstants.TRACK_ENCRYPTED, trackEncrypted.isChecked() ? "1" : "0");
        map.put(PreferencesConstants.EMV_SUPPORT_SM2, checkSmAlgorithm.isChecked() ? "1" : "0");
        return map;
    }

    @Override
    public void showDetail() {
        masterKeyIndex.setText(mPresenter.getParamValue(PreferencesConstants.MASTER_KEY_INDEX));
        trackEncrypted.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.TRACK_ENCRYPTED)));
        checkSmAlgorithm.setChecked("1".equals(mPresenter.getParamValue(PreferencesConstants.EMV_SUPPORT_SM2)));
        //终端主密钥算法类型
        mKeyIndex = mPresenter.getParamValue(PreferencesConstants.MASTER_KEY_ALGORITHM);
        tmkalgoSelection.setSelection(Integer.parseInt(StringUtils.firstNonEmpty(mKeyIndex, "0")));
    }

    @OnClick({R.id.title_iv_left, R.id.title_tv_right})
    public void onClicked(View view) {
        if (UIUtils.isDoubleClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.title_iv_left:
                finish();
                break;
            case R.id.title_tv_right:
                if (mPresenter != null) {
                    mPresenter.saveParams(getDataBind());
                }
                break;
            default:
                break;
        }
    }

    public static Intent newInstance(Context context) {
        return new Intent(context, SetupMasterKeyActivity.class);
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        mKeyIndex = String.valueOf(position);
//        //如果为3DES或者国密算法，则打开磁道加密
//        switch (position) {
//            case 0://EDS
//                trackEncrypted.setChecked(false);
//                break;
//            case 1://3DES
//                trackEncrypted.setChecked(true);
//                break;
//            case 2://国密算法
//                trackEncrypted.setChecked(true);
//                break;
//            default:
//                break;
//        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
